/* ----------------------------------------------------------------------------------------------------------------
 * TextRenderer
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.renderer 
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	import flash.text.TextFormat;
	import pure.engine.system.ns_despair;
	
	use namespace ns_despair;
	
	
	// 文本渲染器
	// [author] - Pure
	
	// [Feature] - 宽高初期化后，无法改变
	// new DropShadowFilter(1, 45, 0, 1, 5, 5, 100, 1)
	
public class TextRenderer extends Renderer implements IText
{
	public function TextRenderer(text:String, offsetX:Number, offsetY:Number, gapX:Number, gapY:Number) 
	{
		super();

		m_textField = new TextField();
		//m_textField.antiAliasType = AntiAliasType.ADVANCED;
		//m_textField.gridFitType = GridFitType.PIXEL;
		//m_textField.sharpness = -244;
		m_textField.autoSize = TextFieldAutoSize.CENTER;
		m_textFormat = new TextFormat();
		//m_textFormat.align = TextFormatAlign.CENTER;
		m_textField.defaultTextFormat = m_textFormat;
		m_offsetX = offsetX;
		m_offsetY = offsetY;
		m_gapX = gapX;
		m_gapY = gapY;
		
		this.text = text;
	}
	

	//======================
	// Member
	//======================

	protected var m_container:Sprite;
	
	protected var m_textField:TextField;
	
	protected var m_textFormat:TextFormat;
	
	protected var m_bgFlag:int;  // [0]无背景，[1]有背景，[2]背景变化
	
	protected var cachedWidth:Number,cachedHeight:Number, m_offsetX:Number, m_offsetY:Number, m_gapX:Number, m_gapY:Number;

	protected var m_fillColor:uint, m_lineColor:uint;
	
	protected var m_fillAlpha:Number, m_ellipse:Number, m_lineThickness:Number, m_lineAlpha:Number;
	
	
	
	
	/**
	 * 设置背景
	 * 
	 * @param fillColor
	 * @param fillAlpha
	 * @param ellipse
	 * @param lineThickness
	 * @param lineColor
	 * @param lineAlpha
	 */
	public function setBackground(fillColor:uint,
								  fillAlpha:Number = 0.5, 
								  ellipse:Number = 0,
								  lineThickness:Number = 0,
								  lineColor:uint = 0,
								  lineAlpha:Number = 1):void
	{
		m_fillColor      =  fillColor;
		m_fillAlpha      =  fillAlpha;
		m_ellipse        =  ellipse;
		m_lineThickness  =  lineThickness;
		m_lineColor      =  lineColor;
		m_lineAlpha      =  lineAlpha;
		m_bgFlag         =  2;
		
		if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
	}
	
	
	/**
	 * 大小
	 */
	public function get size():int{ return int(m_textFormat.size); }
	public function set size(v:int):void 
	{
		if (int(m_textFormat.size) != v)
		{
			m_textFormat.size = v;
			m_textField.setTextFormat(m_textFormat);
			//_rejustVerticalAlign();
			
			if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
		}
	}
	
	
	/**
	 * 颜色
	 */
	public function get color():uint{ return uint(m_textFormat.color); }
	public function set color(v:uint):void 
	{
		if (uint(m_textFormat.color) != v)
		{
			m_textFormat.color = v;
			m_textField.setTextFormat(m_textFormat);
			
			if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
		}
	}
	
	
	/**
	 * 粗细
	 */
	public function get bold():Boolean{ return Boolean(m_textFormat.bold); }
	public function set bold(b:Boolean):void 
	{
		if (Boolean(m_textFormat.bold) != b)
		{
			m_textFormat.bold = b;
			m_textField.setTextFormat(m_textFormat);
			
			if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
		}
	}
	
	
	/**
	 * 字体
	 */
	public function get font():String{ return m_textFormat.font; }
	public function set font(v:String):void 
	{
		if (m_textFormat.font != v)
		{
			m_textFormat.font = v;
			m_textField.setTextFormat(m_textFormat);
			
			if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
		}
	}
	
	
	/**
	 * 内容
	 */
	public function get text():String{ return m_textField.text; }
	public function set text(v:String):void
	{
		if (m_textField.text != v)
		{
			m_textField.text = v;
			//_rejustVerticalAlign();
			if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
		}
	}

	
	/**
	 * 文本滤镜
	 */
	public function get filters():Array { return m_textField.filters; }
	public function set filters(value:Array):void
	{
		m_textField.filters = value;
		
		if (m_dirtyFlag == 0)  m_dirtyFlag = 2;
	}
	
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	
	/**
	 * 调整垂直方向文本对齐
	 */
	protected function _rejustVerticalAlign():void
	{
		m_textField.y = (m_imageA.rect.height - m_textField.textHeight - 4) >> 1;
	}

	
	override protected function _generatePrimaryImage():void
	{
		var tw:Number, th:Number, w:Number, h:Number;
		
		w  =  m_gapX * 2 + (tw = m_textField.width);
		h  =  m_gapY * 2 + (th = m_textField.height);
		
		// 大小变化
		// !!!!!!!!!!!!!!!!!!!!!!!
		if (w != cachedWidth || h != cachedHeight)
		{
			if (m_imageA)  m_imageA.dispose();
			
			cachedWidth   =  w;
			cachedHeight  =  h;
			m_imageA      =  new BitmapData(w, h, true, 0x0);
			m_range       =  RenderUtil.getRange(m_offsetX, m_offsetY, w, h);
			m_x           =  m_offsetX - w * 0.5;
			m_y           =  m_offsetY - h * 0.5;
		}
		
		// 有背景
		// !!!!!!!!!!!!!!!!!!!!!!!
		if (m_bgFlag > 0)
		{
			if (!m_container)
			{
				m_container = new Sprite();
				m_container.addChild(m_textField);
				m_textField.x = m_gapX;
				m_textField.y = m_gapY - 0.5;
			}
			
			if (m_bgFlag == 2)
			{
				m_container.graphics.clear();
				m_container.graphics.beginFill(m_fillColor, m_fillAlpha);
				if (m_lineThickness > 0)
				{
					m_container.graphics.lineStyle(m_lineThickness, m_lineColor, m_lineAlpha);
				}
				m_container.graphics.drawRoundRect(m_lineThickness * 0.5, m_lineThickness * 0.5, cachedWidth - m_lineThickness, cachedHeight - m_lineThickness, m_ellipse, m_ellipse);
			}
			m_imageA.fillRect(m_imageA.rect, 0x0);
			m_imageA.draw(m_container);
		}
		//
		else
		{
			m_imageA.draw(m_textField);
		}
	}
	
	
	override ns_despair function destroy():void
	{
		if (m_imageA)
		{
			m_imageA.dispose();
		}
		if (m_container)
		{
			m_container.removeChild(m_textField);
			m_container = null;
		}
		
		m_textField = null;
		m_textFormat = null;
		
		super.destroy();
	}
}

}